<?php
include_once("_common.php");
include_once("$g4[path]/lib/etc.lib.php");

if ($is_paypal_test)
    $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
else
    $paypal_url = "https://www.paypal.com/cgi-bin/webscr";

$log_file = "$g4[path]/data/log/paypal_$g4[time_ymd]";

if (!$_POST) {
    write_log($log_file, "no post\n");
    exit;
}

$dat = '';
foreach ($_POST as $key => $val) {
    $dat .= "&$key=$val";
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypal_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "cmd=_notify-validate$dat"); //받은 값에서 cmd만 추가해서 그대로 전송
 
$ret = curl_exec($ch);

if (!$ret)
{
    //조회실패
    ob_start();
    echo curl_errno($ch) . curl_error($ch) . "\n";
    $log = ob_get_contents();
    ob_end_clean();

    write_log($log_file, $log);
}
else if (trim($ret) == "VERIFIED")
{
    //전송값 무결성 확인 OK
    ob_start();
    print_r($_POST);
    $log = ob_get_contents();
    ob_end_clean();

    write_log($log_file, $log);

    $is_paypal = true;

    $cl_id = substr($invoice, 1, strlen($invoice));

    $sql = "select * from $mw_cash[cash_list_table] where cl_id = '$cl_id'";
    $row = sql_fetch($sql);
    if (!$row[cl_status]) {
        $mb = get_member($row[mb_id], "mb_nick");
        mw_cash_pg_complete($row[mb_id], $cl_id, $_POST); // 캐쉬충전
        mw_cash_sms($cl_id); // 문자 통보
    }
}

